package me.firstapp.service.zerone.cqssc.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.zerone.cqssc.CqsscLotteryResult;
import me.firstapp.repository.zerone.cqssc.CqsscLotteryResultRepository;
import me.firstapp.service.zerone.cqssc.CqsscLotteryResultService;

@Service
public class CqsscLotteryResultServiceImpl implements CqsscLotteryResultService {

	@Autowired
	private CqsscLotteryResultRepository lotteryResultRepository;

	@Override
	public Page<CqsscLotteryResult> findPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		Page<CqsscLotteryResult> resultPage = lotteryResultRepository.findPage(pageNo, pageSize, searchParams,
				sortParam, sortType);
		return resultPage;
	}

	@Override
	public CqsscLotteryResult add(String issueNo, String prizeNo) {
		CqsscLotteryResult lotteryResult = lotteryResultRepository.findByIssueNo(issueNo);
		if (lotteryResult != null) {
			throw new ServiceException(new StatusObject("error", "期号已存在！"));
		}
		lotteryResult = new CqsscLotteryResult();
		lotteryResult.setIssueNo(issueNo);
		lotteryResult.setPrizeNo(prizeNo);
		String[] arra = issueNo.split("-");
		lotteryResult.setIssueDate(arra[0]);
		lotteryResult.setIssueSubNum(Integer.valueOf(arra[1]));
		lotteryResultRepository.save(lotteryResult);

		return lotteryResult;
	}

	@Override
	public CqsscLotteryResult update(Long id, String issueNo, String prizeNo) {

		CqsscLotteryResult lotteryResult = (CqsscLotteryResult) lotteryResultRepository.getById(id);
		if (StrUtils.isNotNULL(issueNo)) {
			lotteryResult.setIssueNo(issueNo);
			String[] arra = issueNo.split("-");
			lotteryResult.setIssueDate(arra[0]);
			lotteryResult.setIssueSubNum(Integer.valueOf(arra[1]));
		}
		if (StrUtils.isNotNULL(prizeNo)) {
			lotteryResult.setPrizeNo(prizeNo);
		}
		lotteryResultRepository.update(lotteryResult);
		return lotteryResult;
	}

	@Override
	public CqsscLotteryResult findById(Long id) {
		return (CqsscLotteryResult) lotteryResultRepository.getById(id);
	}

}
